const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const baseUrl = require('../controllers/controllers.js').baseUrl;
Page({
  data: {
    portrait:'',
    nick:'',
    mobile:'',
    wechat:'',
    idcardNumber:'',
    infodata:'',
    sexlist:['男','女'],
    sex:'',
    broth:''
  },
  onLoad: function (options) {
    this.info()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value,
      sex:this.data.sexlist[e.detail.value]
    })
  },
  bindPickerChange2: function(e) {
    this.setData({
      broth: e.detail.value
    })
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          infodata:res.data.baseData || '',
          nick:res.data.baseData.nick || '',
          portrait:res.data.baseData.portrait || '',
          sex:res.data.baseData.sex==1?'男':res.data.baseData.sex==2?'女':'',
          broth:res.data.baseData.birthday || '',
        })
      }
    })
  },
  changeinput(e){
    var name=e.currentTarget.dataset.name;
    this.setData({
      [name]:e.detail.value
    })
  },
  upShopLogo: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#222",
      success: function (res) {
        if(!res.cancel) {
          if(res.tapIndex == 0) {
            that.chooseWxImageShop('album');//从相册中选择
          }else if(res.tapIndex == 1) {
            that.chooseWxImageShop('camera');//手机拍照
          }
        }
      }
    })
  },
  chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
        success: function (res) {
        // that.setData({
        //   portrait: res.tempFilePaths[0]
        // })
        console.log(res)
        var img=res.tempFilePaths[0]
        that.picture(img)
      }
    })
  },
  picture(e){
    var that=this;
    wx.uploadFile({
        url: baseUrl+'/upload/picture',
        filePath: e,
        name: "file",
        formData: {
        },
        success: function (res) {
          that.setData({
            portrait:JSON.parse(res.data).data.baseData[0],
          })
        }
    })
  },
  comfirm(e){
    // if (!this.data.portrait) {
    //     wx.showToast({
    //       title: "请选择您的头像",
    //       icon: 'none',
    //       duration: 1000
    //     });
    //     return
    // }
    // if (!this.data.nick) {
    //     wx.showToast({
    //       title: "请输入您的姓名",
    //       icon: 'none',
    //       duration: 1000
    //     });
    //     return
    // }
    // if (!this.data.sex) {
    //     wx.showToast({
    //       title: "请输入您的性别",
    //       icon: 'none',
    //       duration: 1000
    //     });
    //     return
    // }
    // if (!this.data.broth) {
    //     wx.showToast({
    //       title: "请输入您的出生年月日",
    //       icon: 'none',
    //       duration: 1000
    //     });
    //     return
    // }
    var data={
      nick:this.data.nick,
      portrait:this.data.portrait,
      sex:this.data.sex=='男'?1:2,
      birthday:this.data.broth
    }
    Controller.updateInfo(data).then(res=>{
      wx.showToast({
        title: "修改成功",
        icon: 'none',
        duration: 1000
      });
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        })
      },1000)
    })
    
  },
  goadress(){
    wx.navigateTo({
      url:"/pages/mall/adress"
    })
  },
  gopwd(){
    wx.navigateTo({
      url:"/pages/mall/setpwd"
    })
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})